package com.task.core.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.task.common.utils.SystemFunctionUtil;
import com.task.core.domain.DatasourceInfo;
import com.task.core.mapper.DataOperationMapper;
import org.springframework.stereotype.Service;
import org.w3c.dom.ls.LSOutput;

import com.task.common.annotation.DataScope;
import com.task.common.domain.info.KeyAndValue;
import com.task.common.exception.ServiceException;
import com.task.common.utils.SecurityUtils;
import com.task.common.utils.uuid.UUID;
import com.task.core.domain.RefererSourceInfo;
import com.task.core.domain.info.RelationMissionSourceUseDataCountInfo;
import com.task.core.dto.request.RefererSourceRequestDTO;
import com.task.core.mapper.ConversionsMapper;
import com.task.core.mapper.RefererSourceMapper;
import com.task.core.service.RefererSourceService;

/**
 * @author 迪迦.
 * @date 2024/11/4 17:29
 */
@Service("RefererSourceService")
public class RefererSourceServiceImpl implements RefererSourceService {

  @Resource
  private RefererSourceMapper refererSourceMapper;

  @Resource
  private DataOperationMapper dataOperationMapper;

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<RefererSourceInfo> getRefererSourceByPage(RefererSourceRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return refererSourceMapper.getRefererSourceByPage(request);
  }

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<KeyAndValue> getRefererSourceForOption(RefererSourceRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return refererSourceMapper.getRefererSourceForOption(request);
  }

  @Override
  public void addRefererSource(RefererSourceRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    request.setCode(UUID.randomUUID().toString().replace("-", ""));
    refererSourceMapper.addRefererSource(request);
  }

  @Override
  public void modifyRefererSource(RefererSourceRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    Integer integer = refererSourceMapper.modifyRefererSource(request);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
  }

  @Override
  public void deleteRefererSource(String refererSourceCode, Long userId) {
    Integer integer = refererSourceMapper.deleteRefererSource(refererSourceCode, userId);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    //删除对应的关联数据
    for (int i = 1; i <=20 ; i++) {
      String tableName = SystemFunctionUtil.REFERER_TABLE_PREFIX_INFO+i;
      dataOperationMapper.deleteRefereBySourceCode(tableName,refererSourceCode);
    }

    //删除和任务关联的数据
    refererSourceMapper.deleteMissionRefererRelation(refererSourceCode);
    //删除数据使用记录表
    refererSourceMapper.deleteRelationMissionSourceUseDataCount(refererSourceCode);
  }

  @Override
  public void updateDataCount(Integer intValue, String dataSourceCode) {
    refererSourceMapper.updateDataCount(intValue, dataSourceCode);
  }

  @Override
  public RefererSourceInfo getRefererSourceByCode(String code) {
    return refererSourceMapper.getRefererSourceByCode(code);
  }

  @Override
  public List<RelationMissionSourceUseDataCountInfo> getMissionPageRefererInfoByRefererCodes(List<String> sourceCodeList) {
    return refererSourceMapper.getMissionPageRefererInfoByRefererCodes(sourceCodeList);
  }

  @Override
  public List<RefererSourceInfo> getRefererSourceByCodes(List<String> sourceCodeList) {
    return refererSourceMapper.getRefererSourceByCodes(sourceCodeList);
  }

  //@Override
  //public void incrementUseCount(String chooseCode) {
  //  refererSourceMapper.incrementUseCount(chooseCode);
  //}

  //@Override
  //public void decrementUseCount(String sourceCode) {
  //  refererSourceMapper.decrementUseCount(sourceCode);
  //}

}
